package com.ruoyi.web.controller.dating;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.DatingMatch;
import com.ruoyi.system.service.IDatingMatchService;

/**
 * 婚恋匹配记录Controller
 * 
 * @author ruoyi
 * @date 2024-01-15
 */
@RestController
@RequestMapping("/dating/match")
public class DatingMatchController extends BaseController
{
    @Autowired
    private IDatingMatchService datingMatchService;

    /**
     * 查询婚恋匹配记录列表
     */
    @PreAuthorize("@ss.hasPermi('dating:match:list')")
    @GetMapping("/list")
    public TableDataInfo list(DatingMatch datingMatch)
    {
        startPage();
        List<DatingMatch> list = datingMatchService.selectDatingMatchList(datingMatch);
        return getDataTable(list);
    }

    /**
     * 导出婚恋匹配记录列表
     */
    @PreAuthorize("@ss.hasPermi('dating:match:export')")
    @Log(title = "婚恋匹配记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DatingMatch datingMatch)
    {
        List<DatingMatch> list = datingMatchService.selectDatingMatchList(datingMatch);
        ExcelUtil<DatingMatch> util = new ExcelUtil<DatingMatch>(DatingMatch.class);
        util.exportExcel(response, list, "婚恋匹配记录数据");
    }

    /**
     * 获取婚恋匹配记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('dating:match:query')")
    @GetMapping(value = "/{matchId}")
    public AjaxResult getInfo(@PathVariable("matchId") Long matchId)
    {
        return success(datingMatchService.selectDatingMatchByMatchId(matchId));
    }

    /**
     * 新增婚恋匹配记录
     */
    @PreAuthorize("@ss.hasPermi('dating:match:add')")
    @Log(title = "婚恋匹配记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DatingMatch datingMatch)
    {
        return toAjax(datingMatchService.insertDatingMatch(datingMatch));
    }

    /**
     * 修改婚恋匹配记录
     */
    @PreAuthorize("@ss.hasPermi('dating:match:edit')")
    @Log(title = "婚恋匹配记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DatingMatch datingMatch)
    {
        return toAjax(datingMatchService.updateDatingMatch(datingMatch));
    }

    /**
     * 删除婚恋匹配记录
     */
    @PreAuthorize("@ss.hasPermi('dating:match:remove')")
    @Log(title = "婚恋匹配记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{matchIds}")
    public AjaxResult remove(@PathVariable Long[] matchIds)
    {
        return toAjax(datingMatchService.deleteDatingMatchByMatchIds(matchIds));
    }

    /**
     * 自动匹配算法 - 为指定人员寻找最佳匹配
     */
    @PreAuthorize("@ss.hasPermi('dating:match:auto')")
    @Log(title = "自动匹配", businessType = BusinessType.OTHER)
    @PostMapping("/auto/{memberId}")
    public AjaxResult autoMatch(@PathVariable("memberId") Long memberId,
                                @RequestParam(value = "maxCount", defaultValue = "5") int maxCount)
    {
        try {
            List<DatingMatch> matches = datingMatchService.autoMatch(memberId, maxCount);
            return AjaxResult.success("自动匹配完成，为您找到了 " + matches.size() + " 个匹配对象", matches);
        } catch (Exception e) {
            return error("自动匹配失败：" + e.getMessage());
        }
    }

    /**
     * 手动创建匹配
     */
    @PreAuthorize("@ss.hasPermi('dating:match:add')")
    @Log(title = "手动匹配", businessType = BusinessType.INSERT)
    @PostMapping("/manual")
    public AjaxResult manualMatch(@RequestParam("memberIdA") Long memberIdA,
                                  @RequestParam("memberIdB") Long memberIdB,
                                  @RequestParam(value = "remark", required = false) String remark)
    {
        try {
            int result = datingMatchService.createManualMatch(memberIdA, memberIdB, remark);
            if (result > 0) {
                return success("手动匹配创建成功");
            } else {
                return error("匹配创建失败，可能已存在匹配记录或人员不存在");
            }
        } catch (Exception e) {
            return error("手动匹配失败：" + e.getMessage());
        }
    }

    /**
     * 查询指定人员的匹配记录
     */
    @PreAuthorize("@ss.hasPermi('dating:match:query')")
    @GetMapping("/member/{memberId}")
    public AjaxResult getMatchByMemberId(@PathVariable("memberId") Long memberId)
    {
        List<DatingMatch> matches = datingMatchService.selectMatchByMemberId(memberId);
        return success(matches);
    }

    /**
     * 获取匹配统计数据
     */
    @PreAuthorize("@ss.hasPermi('dating:match:list')")
    @GetMapping("/statistics")
    public AjaxResult getStatistics()
    {
        Object stats = datingMatchService.getMatchStatistics();
        return success(stats);
    }

    /**
     * 批量自动匹配 - 为多个人员执行自动匹配
     */
    @PreAuthorize("@ss.hasPermi('dating:match:auto')")
    @Log(title = "批量自动匹配", businessType = BusinessType.OTHER)
    @PostMapping("/batchAuto")
    public AjaxResult batchAutoMatch(@RequestParam("memberIds") Long[] memberIds,
                                     @RequestParam(value = "maxCount", defaultValue = "3") int maxCount)
    {
        try {
            int totalMatches = 0;
            StringBuilder result = new StringBuilder();
            
            for (Long memberId : memberIds) {
                List<DatingMatch> matches = datingMatchService.autoMatch(memberId, maxCount);
                totalMatches += matches.size();
                result.append("人员ID:").append(memberId).append(" 匹配到 ").append(matches.size()).append(" 个对象；");
            }
            
            return AjaxResult.success("批量自动匹配完成，共为 " + memberIds.length + " 个人员创建了 " + totalMatches + " 个匹配记录", result.toString());
        } catch (Exception e) {
            return error("批量自动匹配失败：" + e.getMessage());
        }
    }
} 